/******************************************************************************/
/* ain_def.h                                                                  */
/******************************************************************************/

#ifndef __AIN_DEF_H
#define __AIN_DEF_H


/********************************** Constants *********************************/
#define  AIN1_MASK            (8)
#define  AIN2_MASK            (4)
#define  AIN3_MASK            (2)

#define  AIN1_RESDIV          (14)                        // Res divider for ain1
#define  AIN2_RESDIV          (14)                        // Res divider for ain2
#define  AIN3_RESDIV          (14)                        // Res divider for ain3

#define  AIN1_ADD             (1600)                      // Add ain1
#define  AIN2_ADD             (1600)                      // Add ain2
#define  AIN3_ADD             (1600)                      // Add ain3

#define  AIN1_VKOD            (AIN1_RESDIV*2441+AIN1_ADD) // 1 kod = U
#define  AIN2_VKOD            (AIN2_RESDIV*2441+AIN2_ADD) // 1 kod = U
#define  AIN3_VKOD            (AIN3_RESDIV*2441+AIN3_ADD) // 1 kod = U

#define  ADC_DIV              (10000)                     // Divider for remove weak null

#define  ADC_ON()             AD0CR_bit.PDN = 1
#define  ADC_OFF()            AD0CR_bit.PDN = 0

#define  ADC_START()          AD0CR_bit.START = 1
#define  ADC_STOP()           AD0CR_bit.START = 0

#define  ADC_SET_CH(x)        (AD0CR_bit.SEL = (x))
#define  ADC_TEST             (AD0GDR & 0x80000000)
#define  ADC_DATA()           AD0GDR_bit.RESULT


#define  AIN_ANTIDREBEZG      (15)                        // Antidr value x 200msec

/*************************** Prototypes struct for AIN ************************/
typedef enum
{
    eAIN_1 = 0,                     // Ain 1 (3)
    eAIN_2,                         // Ain 2 (2)
    eAIN_3,                         // Ain 3 (1)
    eAIN_END                        // End token
} __eIND_AIN;                       // Type AIN index


typedef enum
{
    eAIN_NORM_STABLE = 0,           // Ain norm stable state
    eAIN_MAX_ON_ANTI,               // Ain max on-anti
    eAIN_MIN_ON_ANTI                // Ain min on-anti
} __eSTATE_AIN;                     // Type AIN state


#pragma pack(1)
typedef struct
{
    uint8_t    action;
    uint16_t   ainMaxOn;
    uint16_t   ainMaxOff;
} __BAND_AIN;                       // Type AIN band


typedef struct
{
    __BAND_AIN band[4];
    float32_t  koeff;
} __SETT_AIN;                       // Type AIN sett
#pragma pack()


typedef struct
TOOLS_AIN
{
    TOOLS_AIN() : bandState(0), bandIndex(0), koeff(1.0) {
        bands[0] = 0;
        bands[1] = bands[2] = 0xFFFF;
        bands[3] = bands[4] = 0xFFFF;
        bands[5] = bands[6] = 0xFFFF;
        bands[7] = bands[8] = 0xFFFF;
        bands[9] = 0xFFFF;

        action[0] = action[1] = 0;
        action[2] = action[3] = 0;
        action[4] = 0;
    }

    uint16_t  bands[10];
    uint8_t   action[5];
    uint8_t   bandState;
    uint8_t   bandIndex;
    uint8_t   timer;
    float32_t koeff;
} __TOOLS_AIN;                      // Type AIN tools


typedef struct
{
    uint16_t  ainx[eAIN_END];
} __DATA_AIN;                       // Type AIN tools

#endif